import java.util.HashMap;

/**
 * @author wangj881
 * @date 2025-01-02 17:02
 */
public class L560_3 {

    public static void main(String[] args) {
        int[] nums = {1, 2, 3};
//        int[] nums = {-1, -1, 1};
//        int[] nums = {1};
        int k = 3;
        int i = new Solution().subarraySum(nums, k);
        System.out.println(i);

    }

    static class Solution {
        public int subarraySum(int[] nums, int k) {
            int result = 0;
            int sum = 0;
            HashMap<Integer, Integer> map = new HashMap<>();
            map.put(0, 1);
            for (int num : nums) {
                sum += num;
                if (map.containsKey(sum - k)) {
                    result += map.get(sum - k);
                }
                map.put(sum, map.getOrDefault(sum, 0) + 1);
            }

            return result;
        }
    }
}
